package com.oa.admin.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.joneying.common.web.response.Resp;
import com.oa.admin.entity.Holiday;
import com.oa.admin.entity.UserHoliday;
import com.oa.admin.mapper.HolidayMapper;
import com.oa.admin.service.IHolidayService;
import com.oa.admin.service.IUserHolidayService;
import com.oa.admin.vo.UserHolidayListVO;
import com.oa.system.entity.User;
import com.oa.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HolidayServiceImpl extends ServiceImpl<HolidayMapper, Holiday> implements IHolidayService {

    @Autowired
    private HolidayMapper holidayMapper;

    @Autowired
    private IUserHolidayService userHolidayService;

    @Autowired
    private IUserService userService;

    @Override
    public Resp updateUser() {
        List<User> users = userService.selectList(new EntityWrapper().eq("is_deleted", 2));

        List<Holiday> holidays = holidayMapper.selectList(new EntityWrapper<>());

        for (User user : users) {
            userHolidayService.delete(new EntityWrapper().eq("user_id", user.getId()));
            List<UserHoliday> userHolidays = new ArrayList<>();
            for(int i = 0; i < holidays.size(); i++){
                UserHoliday userHoliday = new UserHoliday();
                userHoliday.setSurplusDay(holidays.get(i).getHolidayNumber());
                userHoliday.setAlreadyUsedDay(0.0);
                userHoliday.setUserId(user.getId());
                userHoliday.setHolidayId(holidays.get(i).getId());
                userHolidays.add(userHoliday);
            }
            userHolidayService.insertBatch(userHolidays);
        }

        return new Resp().success();
    }
}
